package com.open.party.cloud.view.examine.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.open.party.cloud.R;
import com.open.party.cloud.model.ExamineClassEntity;
import com.sinothk.android.utils.XUtils;
import com.sinothk.android.utils.inters.OnSuperListener;

import java.util.ArrayList;
import java.util.List;

public class ExamineClassForMineListAdapter extends RecyclerView.Adapter<ExamineClassForMineListAdapter.ViewHolder> {

    // 展示数据
    protected ArrayList<ExamineClassEntity> mData;
    private Context mContext;
    private OnSuperListener<ExamineClassEntity> onSuperListener;

    public ExamineClassForMineListAdapter(Context mContext) {
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

    public void setOnSuperListener(OnSuperListener<ExamineClassEntity> onSuperListener) {
        this.onSuperListener = onSuperListener;
    }

    public void setData(List<ExamineClassEntity> data) {
        if (mData == null) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public void updateData(List<ExamineClassEntity> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.examine_class_list_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // 绑定数据
        final ExamineClassEntity itemData = mData.get(position);

        holder.titleTv.setText(XUtils.string().getNotNullValue(itemData.getClassName()));

        holder.adminNameTv.setText("班主任：" + XUtils.string().getNotNullValue(itemData.getAdminName()));
        holder.phoneNumTv.setText("联系电话：" + XUtils.string().getNotNullValue(itemData.getTechPhone()));

        holder.startTimeTv.setText("开始时间：" + XUtils.string().getNotNullValue(itemData.getStartTime()));
        holder.endTimeTv.setText("结束时间：" + XUtils.string().getNotNullValue(itemData.getEndTime()));

        holder.zbdwTv.setText("主办单位：" + XUtils.string().getNotNullValue(itemData.getZbdw()));
        holder.pxjgTv.setText("培训机构：" + XUtils.string().getNotNullValue(itemData.getPxjg()));
        holder.userCountTv.setText("学生人数：" + itemData.getUserCount() + "人");

//        if ("ZC".equals(itemData.getState())) {
//            holder.stateTv.setText("正常");
//            holder.stateTv.setTextColor(Color.parseColor("#14F83A"));
//        } else if ("YJY".equals(itemData.getState())) {
//            holder.stateTv.setText(":已结业");
//            holder.stateTv.setTextColor(Color.parseColor("#F22119"));
//        } else {
//            holder.stateTv.setText("未知状态");
//            holder.stateTv.setTextColor(Color.parseColor("#666666"));
//        }

//        state 状态：noJoin：未加入；audit:待审核；pass：审核通过；back：驳回、

        holder.joinBtn.setVisibility(View.INVISIBLE);
        if ("noJoin".equals(itemData.getState())) {
            holder.stateTv.setText("未加入");
            holder.stateTv.setTextColor(Color.parseColor("#F27D19"));

            holder.joinBtn.setVisibility(View.VISIBLE);
            holder.joinBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onSuperListener.onClick(position, itemData, "joinBtn");
                }
            });
        } else if ("audit".equals(itemData.getState())) {
            holder.stateTv.setText("待审核");
            holder.stateTv.setTextColor(Color.parseColor("#F22119"));
        } else if ("pass".equals(itemData.getState())) {
            holder.stateTv.setText("已加入");
            holder.stateTv.setTextColor(Color.parseColor("#14F83A"));

        } else if ("back".equals(itemData.getState())) {
            holder.stateTv.setText("驳回");
            holder.stateTv.setTextColor(Color.parseColor("#F22119"));
            holder.joinBtn.setVisibility(View.VISIBLE);
            holder.joinBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onSuperListener.onClick(position, itemData, "joinBtn");
                }
            });
        } else {
            holder.stateTv.setText("未知状态");
            holder.stateTv.setTextColor(Color.parseColor("#F22119"));
        }

        if (mData.size() - 1 == position) {
            holder.bottomLineTv.setVisibility(View.VISIBLE);
        } else {
            holder.bottomLineTv.setVisibility(View.GONE);
        }

        holder.remarkTv.setText("备\u3000\u3000注："+XUtils.string().getNotNullValue(itemData.getRemark()));

        if (onSuperListener != null) {
            holder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onSuperListener.onClick(position, itemData, "rootView");
                }
            });
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout rootView;
        TextView titleTv, adminNameTv, phoneNumTv, startTimeTv, stateTv, bottomLineTv, zbdwTv, pxjgTv, endTimeTv, userCountTv, joinBtn, remarkTv;

        ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);

            titleTv = itemView.findViewById(R.id.titleTv);
            adminNameTv = itemView.findViewById(R.id.adminNameTv);
            phoneNumTv = itemView.findViewById(R.id.phoneNumTv);
            startTimeTv = itemView.findViewById(R.id.startTimeTv);
            zbdwTv = itemView.findViewById(R.id.zbdwTv);
            pxjgTv = itemView.findViewById(R.id.pxjgTv);
            userCountTv = itemView.findViewById(R.id.userCountTv);

            endTimeTv = itemView.findViewById(R.id.endTimeTv);

            stateTv = itemView.findViewById(R.id.stateTv);
            joinBtn = itemView.findViewById(R.id.joinBtn);
            remarkTv = itemView.findViewById(R.id.remarkTv);

            bottomLineTv = itemView.findViewById(R.id.bottomLineTv);
        }
    }
}